package com.booktrade.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.crypto.SecureUtil;
import com.booktrade.dto.UsersRegisterDTO;
import com.booktrade.entity.Users;
import com.booktrade.mapper.UsersMapper;
import com.booktrade.service.IUsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表，存储平台所有用户信息 服务实现类
 * </p>
 *
 */
@Service
@RequiredArgsConstructor
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {
    private final UsersMapper usersMapper;
    @Override
    public void register(UsersRegisterDTO usersRegisterDTO) {
        Users users = this.lambdaQuery().eq(Users::getUsername, usersRegisterDTO.getUsername()).one();
        Assert.isNull(users, "用户名已存在");
        users = BeanUtil.copyProperties(usersRegisterDTO, Users.class);
        users.setPassword(SecureUtil.md5(users.getPassword()));
        this.save(users);
    }
}
